package com.mmall.controller.portal;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.demo.trade.config.Configs;
import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.krb5.Config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //支付操作
    @ResponseBody
    @RequestMapping("payOrder")
    public ServerResponse payOrder(HttpSession session, long orderNumber, HttpServletRequest request) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(), "请先登录");
        }
        String path = session.getServletContext().getRealPath("upload");
        return orderService.pay(user.getId(), orderNumber, path);
    }

    //该方法交给支付宝调用
    @ResponseBody
    @RequestMapping("alipayCallBack")
    public Object payCallBack(HttpServletRequest request) {
        Map requestParameterMap = request.getParameterMap();
        Map<String, String> params = Maps.newHashMap();
        for (Iterator iter = requestParameterMap.entrySet().iterator(); iter.hasNext(); ) {
            String key = (String) iter.next();
            String[] values = (String[]) requestParameterMap.get(key);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == valueStr.length() - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(key, valueStr);
        }
        params.remove("sign");
        boolean success = false;
        try {
            success = AlipaySignature.rsaCheckV2(params, Configs.getPublicKey(), "utf-8", Configs.getSignType());
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (!success) {
            return ServerResponse.createServerResponseByFailure("非法请求!");
        }

        ServerResponse serverResponse = orderService.payCallBack(params);
        if (serverResponse.checkIsSuccess()) {
            return Const.AlipayCallBack.RESPONSESUCCESS;
        } else {
            return Const.AlipayCallBack.RESPONSEFAIL;
        }
    }
    //查看订单状态
    @ResponseBody
    @RequestMapping("queryOrderStatus")
    public ServerResponse<Boolean> queryOrderStatus(HttpSession session, long orderNumber, HttpServletRequest request) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(), "请先登录");
        }

        ServerResponse serverResponse =  orderService.queryOrderStatus(user.getId(), orderNumber);
        if(serverResponse.checkIsSuccess()){
            return ServerResponse.createServerResponseBySuccess(true);
        }
        else{
            return ServerResponse.createServerResponseBySuccess(false);
        }

    }
    //提交订单
    public ServerResponse<Boolean> admitOrder(HttpSession session, long orderNumber, HttpServletRequest request) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(), "请先登录");
        }

        //从数据库查询已经勾选的商品


        return null;
    }




}

